package com.dk.config;

import com.dk.shiro.UserRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//shiro配置类
@Configuration
public class ShiroConfig {
    //创建 ShiroFilterFactoryBean 过滤器链
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Autowired DefaultWebSecurityManager defaultWebSecurityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
//        设置安全管理器
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
        return shiroFilterFactoryBean;
    }

    //    创建 DefaultWebSecurityManager 安全管理器实例化
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager() {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
//        关联realm
        defaultWebSecurityManager.setRealm(getSellerRealm());
        return defaultWebSecurityManager;
    }

    //    创建Realm shiro连接数据的桥梁
    @Bean
    public UserRealm getSellerRealm() {
        UserRealm userRealm = new UserRealm();
        return userRealm;
    }
}
